<template>
    <div class="section_common">
        <a-spin  :spinning="loading" />
        <div class="padding_button">
            <span class="demonstration">查看日期 </span>
            <el-date-picker
                    v-model="biDate"
                    type="daterange"
                    range-separator="至"
                    value-format="yyyy-MM-dd"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
            </el-date-picker>
            <el-button class="padding_button" type="primary" plain @click="getApprovalDayData"
                       element-loading-text="拼命生成文件中"
                       v-loading.fullscreen.lock="loading">点击下载EXCEL查看</el-button>
        </div>

        <div class="padding_button">
            <span class="demonstration">更新日期 </span>
            <el-date-picker
                    v-model="updateTime"
                    type="daterange"
                    range-separator="至"
                    value-format="yyyy-MM-dd"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
            </el-date-picker>
            <el-button class="padding_button" type="primary" plain @click="updateFromWX">点击更新</el-button>
        </div>
    </div>
</template>

<script>
    import {Dialog} from "vant";

    export default {
        data() {
            return {
                loading:false,
                biDate: '', // 选择的日期
                getApprovalDayDataUrl: '/approve/pageCheckInDayDataMobile',
                getDataFromWXUrl: '/approve/getDataFromWX',
                userInfo: JSON.parse(localStorage.getItem("userInfo")),
                updateTime: [],
            }
        },
        methods: {
            //下载Excel
            getApprovalDayData() {
                var that = this;
                if (that.biDate == null || that.biDate == '') {
                    Dialog({message: "日期不可为空"});
                    return false;
                }
                that.loading = true;
                this.$https
                    .post(that.getApprovalDayDataUrl, {
                        user_id: that.userInfo.userId,
                        biDate: that.biDate,
                    })
                    .then(res => {
                        if (res.data.code != 200) {
                            that.loading = false;
                            Dialog({message: res.data.message});
                            return false;
                        }
                        that.export2Excel(res.data.data);
                        console.log(res);
                        that.loading = false;
                    })
                    .catch(err => {
                        that.loading = false;
                        Dialog({message: err});
                        console.log(err);
                    });

            },

            //更新数据
            updateFromWX() {
                var that = this;
                if (that.updateTime == null || that.updateTime == '') {
                    Dialog({message: "日期不可为空"});
                    return false;
                }

                var startTime = that.updateTime[0];
                var endTime = startTime;
                if (that.updateTime.length > 1) {
                    endTime = that.updateTime[1];
                }
                this.$https
                    .post(that.getDataFromWXUrl, {
                        startTime: new Date(startTime).getTime()/1000,
                        endTime: new Date(endTime).getTime()/1000,
                    })
                    .then(() => {
                        Dialog({message: "更新可能需要几分钟时间，在此期间您可以进行其它操作。请尽量避免重复点击按钮更新。"});
                    })
                    .catch(err => {
                        Dialog({message: "更新失败，请重新更新"});
                        console.log(err);
                    });
            },

            //导出excel数据
            export2Excel(list) {
                const tHeader = ['姓名', '部门', '日期', '排班', '打卡数', '实际工作时长(不包含请假时间)', '标准工作时长', '最早打卡时间', '最晚打卡时间', '假勤描述', '假勤时间段', '加班时间'];
                const filterVal = ['bi_name', 'bi_departs_name', 'bi_date', 'biri_schedule_shift', 'si_checkin_count', 'si_regular_work_sec', 'si_standard_work_sec', 'si_earliest_time', 'si_lastest_time', 'hist_data_text', 'hisd_data_text', 'overTime'];
                const data = this.formatJson(filterVal, list);
                const {export_json_to_excel} = require('@/views/hrmanager/excel/export2Excel')
                export_json_to_excel(tHeader, data, '考勤日报表' + this.biDate);
            },
            formatJson(filterVal, jsonData) {
                return jsonData.map(v => filterVal.map(j => v[j]))
            },
        },


    };
</script>

<style lang="scss" scoped>
    .padding_button {
        margin: 20px;
    }
</style>
